/**
 * 战绩回放
 * Created by duwei on 2018/6/8.
 */
var RecordPlayBackWindow = Window.extend({
    _ccsFileName:res.RecordPlayBackWindow_json,
    /**
     * 数字数组
     * @type {Array}
     */
    _nums: [],
    /**
     * 初始化
     */
    initWindow:function(){
        this._nums = [];
        this._changeNum();
    },
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'btn_node.0': '_clickNum',
            'btn_node.1': '_clickNum',
            'btn_node.2': '_clickNum',
            'btn_node.3': '_clickNum',
            'btn_node.4': '_clickNum',
            'btn_node.5': '_clickNum',
            'btn_node.6': '_clickNum',
            'btn_node.7': '_clickNum',
            'btn_node.8': '_clickNum',
            'btn_node.9': '_clickNum',
            'btn_node.btn_delete': '_clickDel',
            'btn_node.btn_reInput': '_clickReset',
            'btn_ok': '_clickOk'
        };
    },
    /**
     * 点击确认
     * @private
     */
    _clickOk: function () {
        var self = this;
        var recId = _.trim(self.cunNum);
        if (!recId){
            return showAlert('请输入正确的回放码');
        }
        var recordId = recId.substr(0, recId.length - 1) + '-' + recId.charAt(recId.length-1);
        var recordIdA = recId.substr(0, recId.length - 2) + '-' + recId.charAt(recId.length-2) + recId.charAt(recId.length-1);
        var replayurl = app.recordPath[1] + '/' + app.recordPath[2];
        showLoading("努力加载中..");
        var func = function (data) {
            hideLoading();
            var tData = data.replace(/'/g, "\"");
            data = JSON.parse(tData);
            if (data['3002']) {
                gameData.mapId = data['3002']['data']['map_id'];
            } else {
                var option = JSON.parse(data[0]['data']['Option']);
                gameData.mapId = option['mapid'];
            }
            SubUpdateUtil.checkUpdateSubByMapId(gameData.mapId,function (sub) {
                showRoom(data, ROOM_DATA_TYPE.REPLAY);
            });
        };
        http.get(replayurl + recordId, function (data) {
            //用于播放下一局
            gameData.recordIds = recordId.split('-');
            func(data);
        }, function () {
            http.get(replayurl + recordIdA, function (data) {
                //用于播放下一局
                gameData.recordIds = recordIdA.split('-');
                func(data);
            }, function () {
                hideLoading();
                showAlert('获取回放失败, 回放码不正确或已失效');
            });
        });
    },
    /**
     * 点击数字
     * @private
     */
    _clickNum: function (sender) {
        this._nums.push(sender.getName());
        this._changeNum();
    },
    /**
     * 点击删除
     * @private
     */
    _clickDel: function () {
        this._nums.pop();
        this._changeNum();
    },
    /**
     * 点击清除
     * @private
     */
    _clickReset: function () {
        this._nums = [];
        this._changeNum();
    },
    /**
     * 改变当前的数字
     * @private
     */
    _changeNum: function () {
        var self = this;
        self.cunNum = self._nums.join('');
        self._nodeList['numbg']["num"].setString(self.cunNum);
    }
});